package dao

import (
	"dc-news/dao/cache"
	"dc-news/dao/db"
	"dc-news/dao/model"
	"dc-news/entity"
	"go.uber.org/zap"
)

// GetUserByEmail 通过邮箱获取用户
func GetUserByEmail(email string) (user *entity.User, err error) {
	return db.GetUserByEmail(email)
}

// GetUserByUsername 通过用户名获取用户
func GetUserByUsername(username string) (user *entity.User, err error) {
	return db.GetUserByUsername(username)
}

// ChangePasswordById 通过id修改密码
func ChangePasswordById(id int, password string) (err error) {
	return db.ChangePasswordById(id, password)
}

// CreateUser 创建用户
func CreateUser(user *entity.User) error {
	return db.CreateUser(user)
}

// CheckUsernameAndEmail 查看是否存在该用户
func CheckUsernameAndEmail(username string, email string) (exist bool) {
	return db.CheckUsernameAndEmail(username, email)
}

// ConfirmEmailByUserId 确认邮件
func ConfirmEmailByUserId(id int) (err error) {
	return db.ConfirmEmailByUserId(id)
}

// GetUserById 通过id获取用户
func GetUserById(id int) (user *entity.User, err error) {
	return db.GetUserById(id)
}

// GetUserSettingById 通过id获取用户设置
func GetUserSettingById(id int) (setting *entity.UserSetting, err error) {
	return db.GetUserSettingById(id)
}

// UpdateUserSettingById 通过id更新用户设置
func UpdateUserSettingById(id int, setting *entity.UserSetting) (err error) {
	return db.UpdateUserSettingById(id, setting)
}

// UpdateUserInfoById 通过id更新用户信息
func UpdateUserInfoById(id int, user *entity.User) (err error) {
	return db.UpdateUserInfoById(id, user)
}

// GetUserIsMutedById 通过id查询用户是否禁言
func GetUserIsMutedById(id int) (isMuted bool, err error) {
	return db.GetUserIsMutedById(id)
}

// GetUseReportById 通过id获取用户使用报告
func GetUseReportById(id int) (report *model.Report, err error) {
	report, err = cache.GetUseReportById(id)
	if err != nil {
		report = db.GetUseReportById(id)
		return report, nil
	}
	if report == nil {
		report = db.GetUseReportById(id)
		_ = cache.SaveReportById(id, report)
		zap.S().Debug("report from db")
		return
	}
	zap.S().Debug("report from redis")
	return
}
